package com.viettel.pon.DAO;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.log4j.Logger;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.transport.HttpsTransportSE;

import android.util.Log;

import com.viettel.pon.config.Config;
import com.viettel.pon.form.InputLoginForm;
import com.viettel.pon.form.ResultLoginForm;


public class AuthenDAO {
	
	
	
	public ResultLoginForm Authenticate(String username,String password,Logger mLog) {
	
		ResultLoginForm resultForm=new ResultLoginForm();
		String SOAP_ACTION1 = Config.SOAP_ACTION1;

		String NAMESPACE = Config.NAMESPACE;

		String METHOD_NAME1 =Config.METHOD_NAME1;

		String URL = Config.URL;

		String strResult = "";
		// Initialize soap request + add parameters
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
		// Use this to add parameters

//		List<String> test = Arrays.asList(stationIds.split(","));
		// request.addProperty("stationIds", "IPBN");
//		for (String s : test) {
			// myList.add(Long.parseLong(s));
//			request.addProperty("username", username);
//			request.addProperty("password", password);
//			Log.e("username", String.valueOf(username));
//			Log.e("password", String.valueOf(password));
//		}
		InputLoginForm input=new InputLoginForm();
		input.setUsername(username);
		input.setPassword(password);
		
		 PropertyInfo listProp =new PropertyInfo();
		 listProp.setName("inputLoginForm");
		 listProp.setValue(input);
		 listProp.setType(input.getClass());
		// request.addProperty("stationIds","67933");
		 request.addProperty(listProp);
		// Declare the version of the SOAP request
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
		envelope.dotNet = false;
		try {
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//			HttpsTransportSE androidHttpTransport = new HttpsTransportSE(Config.URL_LINK,8081,Config.URL_METHOD,15000);
			// this is the actual part that will call the webservice
			androidHttpTransport.call(SOAP_ACTION1, envelope);
			// Get the SoapResult from the envelope body.
			SoapObject result = null;
			if (envelope.bodyIn instanceof SoapFault) {
				String str = ((SoapFault) envelope.bodyIn).faultstring;
				Log.e("result", str);
			} else {
				result = (SoapObject) envelope.bodyIn;
			}
			if (result != null) {
			Log.e("total", String.valueOf(result.getPropertyCount()));
			for (int i = 0; i < result.getPropertyCount(); i++) {
				Object property = result.getProperty(i);
				if (property instanceof SoapObject) {
					SoapObject resultList = (SoapObject) property;
					String deptId=resultList.getPropertySafely("departmentId", "").toString();
					if(deptId!=null && !deptId.equals("")){
					resultForm.setDepartmentId(Long.parseLong(deptId));
					}
					resultForm.setDeptName(resultList.getPropertySafely("deptName", "").toString());
					resultForm.setErrorCode(resultList.getPropertySafely("errorCode", "").toString());
					resultForm.setFullname(resultList.getPropertySafely("fullname", "").toString());
					resultForm.setSessionId(resultList.getPropertySafely("sessionId", "").toString());
				}
			}
			} else {
				resultForm.setErrorCode("F");
			}

		} catch (Exception e) {
			e.printStackTrace();
			mLog.error("Error when call WS Login", e);
			resultForm.setErrorCode("F");
		}
		return resultForm;
	}

	
}
